package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.concurrent.Callable;

@RestController("backendCheckGroupController")
@Slf4j
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    CheckGroupService checkGroupService;
    CheckItemService checkItemService;
    //添加检查组++++++
   @PostMapping("/add")
    Result addGroupByItemId(@RequestBody CheckGroup checkGroup, @RequestParam ArrayList<Integer>checkitemIds){
       log.info("新增检查组：{},{}", checkGroup,checkitemIds);
       /*CheckGroup temp=checkGroupService.getCheckGroup(checkGroup);
        if(temp==null){
            checkGroupService.addCheckGroup(checkGroup);}
        CheckGroup resTemp=checkGroupService.getCheckGroup(checkGroup);

        for(int i=0;i<ids.size();i++){
         checkGroupService.addGroupById(resTemp.getId(),ids.get(i));}*/
       checkGroupService.addCheckGroup(checkGroup,checkitemIds);
        return  new Result(true,"新增检查组成功");}
    //删除检查组----------------------
   @GetMapping("/deleteById")
    Result deleteById(int id){
       log.info("删除检查组：{}", id);
    checkGroupService.deleteById(id);
       return new Result(true,"删除检查组成功");
   }
   //查询套餐下的所有id--------------------
   @GetMapping("/findCheckGroupIdsBySetmealId")
   Result findCheckGroupIdsById(int setmealId){
       log.info("查询套餐id为：{} 下的所有检查组id", setmealId);
        ArrayList<Integer>resultList=checkGroupService.findCheckGroupIdsById(setmealId);

       return new Result(true,"查找检查组成功",resultList);
   }
   //分页查询------
   @PostMapping("/findPage")
   PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       log.info("分页查询检查组：{}", queryPageBean);
       PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
       return  pageResult ;
   }
   //编辑检查组--------------------------
    @PostMapping("/edit")
    Result UpdateByItemIds(@RequestBody CheckGroup checkGroup,@RequestParam ArrayList<Integer>checkitemIds){
        log.info("编辑检查组：{},{}", checkGroup,checkitemIds);
        checkGroupService.updateCheckGroup(checkGroup,checkitemIds);
        return new Result(true,"编辑检查组成功");
    }



//查询所有检查组----------------------------
    @GetMapping("/findAll")
    Result findAll(){
        log.info("查询所有检查组");
       ArrayList<CheckGroup>list=checkGroupService.findAll();
        return new Result(true,"查找全部检查组成功",list);
    }
    //查询检查组通过id----------------
    @GetMapping("findById")
   Result findById(int id){
        log.info("根据id查询检查组：{}", id);
       CheckGroup checkGroup
        =checkGroupService.findById(id);
       return  new Result(true,"查找检查组成功",checkGroup);
    }
}
